package webServices;

import javax.jws.*;
import javax.jws.soap.*;

import cern.jet.random.Poisson;
import cern.jet.random.engine.MersenneTwister;

@WebService(targetNamespace = "http://www.airline.com/", serviceName = "Airline Reservation")
@SOAPBinding(style = SOAPBinding.Style.RPC)  
@HandlerChain(file = "../../META-INF/jaxws-handlers-server.xml")
public class AirlineReservation {
	
	private static final double MRT_CHECK_FLIGHTS=140.0;
	private static final double MRT_FLIGHT_DETAILS=25.0;
	private static final double MRT_CANCEL_RESERVATION=200.0;
	private static final double MRT_RESERVE_FLIGHT=200.0;
	private static final double MRT_CONFIRM_FLIGHT_RESERVATION=200.0;
	private static final double MRT_TICKET_DETAILS=25.0;
	private Poisson p_checkFlights=null;
	private Poisson p_flightDetails=null;
	private Poisson p_cancelReservation=null;
	private Poisson p_reserveFlight=null;
	private Poisson p_ConfirmFlightReservation=null;
	private Poisson p_ticketDetails=null;
	
	@WebMethod
	@WebResult(name="available flights")
	public void checkFlights(	@WebParam(name="sequence_number",header=true) long sequence_number, 
								@WebParam(name="username",header=true) String username, 
								@WebParam(name="password",header=true) String password){
		if(p_checkFlights==null)
			p_checkFlights = new Poisson(MRT_CHECK_FLIGHTS, new MersenneTwister(new Long(System.currentTimeMillis()).intValue()));
		try {
			int num=p_checkFlights.nextInt();
			Thread.sleep(num);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	@WebMethod
	@WebResult(name="flight details")
	public void getFlightDetails(@WebParam(name="sequence_number",header=true) long sequence_number, 
								@WebParam(name="username",header=true) String username, 
								@WebParam(name="password",header=true) String password){
		if(p_flightDetails==null)
			p_flightDetails = new Poisson(MRT_FLIGHT_DETAILS, new MersenneTwister(new Long(System.currentTimeMillis()).intValue()));
		try {
			int num=p_flightDetails.nextInt();
			Thread.sleep(num);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	@WebMethod
	@WebResult(name="cancel reservation")
	public void cancelReservation(	@WebParam(name="sequence_number",header=true) long sequence_number, 
									@WebParam(name="username",header=true) String username, 
									@WebParam(name="password",header=true) String password){
		if(p_cancelReservation==null)
			p_cancelReservation = new Poisson(MRT_CANCEL_RESERVATION, new MersenneTwister(new Long(System.currentTimeMillis()).intValue()));
		try {
			int num=p_cancelReservation.nextInt();
			Thread.sleep(num);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	@WebMethod
	@WebResult(name="reserve flight")
	public void reserveFlight(	@WebParam(name="sequence_number",header=true) long sequence_number, 
								@WebParam(name="username",header=true) String username, 
								@WebParam(name="password",header=true) String password){
		if(p_reserveFlight==null)
			p_reserveFlight = new Poisson(MRT_RESERVE_FLIGHT, new MersenneTwister(new Long(System.currentTimeMillis()).intValue()));
		try {
			int num=p_reserveFlight.nextInt();
			Thread.sleep(num);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	@WebMethod
	@WebResult(name="confirm and pay flight")
	public void  confirmFlightReservation(	@WebParam(name="sequence_number",header=true) long sequence_number, 
											@WebParam(name="username",header=true) String username, 
											@WebParam(name="password",header=true) String password){
		if(p_ConfirmFlightReservation==null)
			p_ConfirmFlightReservation = new Poisson(MRT_CONFIRM_FLIGHT_RESERVATION, new MersenneTwister(new Long(System.currentTimeMillis()).intValue()));
		try {
			int num=p_ConfirmFlightReservation.nextInt();
			Thread.sleep(num);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	@WebMethod
	@WebResult(name="ticket details")
	public void getTicketDetails(	@WebParam(name="sequence_number",header=true) long sequence_number, 
									@WebParam(name="username",header=true) String username, 
									@WebParam(name="password",header=true) String password){
		if(p_ticketDetails==null)
			p_ticketDetails = new Poisson(MRT_TICKET_DETAILS, new MersenneTwister(new Long(System.currentTimeMillis()).intValue()));
		try {
			int num=p_ticketDetails.nextInt();
			Thread.sleep(num);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
